import { queryStationList, queryCompanyList, queryTestingUnitList, getInsertStationInfo, getCompanyList, deleteCompanyInfo } from '@/api/dict.js'

export default {
  components: {},
  name: 'ConventionalUnitEdit',
  props: {
    detailForm: {
      type: Object,
      default: () => {},
    },
    dispatchList: {
      type: Array,
      default: () => [],
    },
    stationTypeLists: {
      type: Array,
      default: () => [],
    },
    operate: {
      type: String,
    },
  },
  data() {
    let validateSingleMachineCap = (rule, value, callback) => {
      var reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g
      if (!reg.test(value)) {
        callback(new Error('只能输入数字和小数点,小数点后最多保留3位'))
      } else {
        callback()
      }
    }

    let validateUnitsNum = (rule, value, callback) => {
      var reg = /^([0-9]+|0)$/
      if (!reg.test(value)) {
        callback(new Error('请输入数字'))
      } else {
        callback()
      }
    }

    return {
      stationIdError: '',
      regulationTime1: '',
      order: '场站',
      getInsertStationInfo,
      getCompanyList,
      deleteCompanyInfo,
      stationLists: [], //电厂名称
      companyLists: [], //控股单位
      formLabelWidth: '130px',
      // 上传
      formData: new FormData(), //载体
      file: {},
      fileList1: [], //展示的文件列表,
      fileList2: [], //展示的文件列表,
      fileList3: [], //展示的文件列表,
      fileList4: [], //展示的文件列表,
      fileList5: [], //展示的文件列表,

      flagUpload: true,

      isYes: [
        {
          label: "是",
          value: '1',
        },
        {
          label: "否",
          value: '0',
        },
      ],

      //   校验
      rules: {
        singleMachineCap: [
          { required: true, message: '请输入单机容量', trigger: 'change' },
          { validator: validateSingleMachineCap, trigger: 'change' },
        ],
        machineNum: [
          { required: true, message: '请输入台数', trigger: 'change' },
          { validator: validateUnitsNum, trigger: 'change' },
        ],
        regulationOrgId: [
          {
            required: true,
            message: '请选择所属调管调度机构',
            trigger: 'change',
          },
        ],
        stationType: [{ required: true, message: '请选择', trigger: 'change' }],
        regulationOrgId: [{ required: true, message: '请选择', trigger: 'change' }],
        unitsNumber: [{ required: true, message: '请输入机组编号', trigger: 'change' }],
      },
    }
  },

  computed: {},

  created() {
    this.queryCompanyList()

    if (this.operate === 'edit' || this.operate === 'copy') {
      this.queryStationList()
    }

    // 前台回显总容量
    if (this.operate === 'edit' || this.operate === 'copy') {
      this.detailForm.unitsTotalCap = this.detailForm.singleMachineCap * this.detailForm.machineNum
    }

    // 文件上传的回显  时间的回显
    if (this.detailForm.regulationReportForm) {
      if (this.detailForm.regulationReportForm.createTime) {
        this.detailForm.regulationTime1 = this.detailForm.regulationReportForm.createTime.replace('T', ' ')
      }
      this.detailForm.regulationReportForm = this.detailForm.regulationReportForm.oldName
    }

    if (this.detailForm.regulationTestPlan) {
      if (this.detailForm.regulationTestPlan.createTime) {
        this.detailForm.regulationTime2 = this.detailForm.regulationTestPlan.createTime.replace('T', ' ')
      }
      this.detailForm.regulationTestPlan = this.detailForm.regulationTestPlan.oldName
    }
    if (this.detailForm.regulationTestReport) {
      if (this.detailForm.regulationTestReport.createTime) {
        this.detailForm.regulationTime3 = this.detailForm.regulationTestReport.createTime.replace('T', ' ')
      }
      this.detailForm.regulationTestReport = this.detailForm.regulationTestReport.oldName
    }

    // if (this.detailForm.responsePlan) {
    //   if (this.detailForm.responsePlan.createTime) {
    //     this.detailForm.regulationTime4 =
    //       this.detailForm.responsePlan.createTime.replace("T", " ");
    //   }
    //   this.detailForm.responsePlan = this.detailForm.responsePlan.oldName;
    // }

    // if (this.detailForm.responseReport) {
    //   if (this.detailForm.responseReport.createTime) {
    //     this.detailForm.regulationTime5 =
    //       this.detailForm.responseReport.createTime.replace("T", " ");
    //   }
    //   this.detailForm.responseReport = this.detailForm.responseReport.oldName;
    // }
  },

  computed: {
    disabledSelect() {
      return this.operate === 'edit'
    },
  },

  methods: {
    regulationReportFormChange() {
      if (!this.detailForm.regulationTestPlan || !this.detailForm.regulationTestReport) {
        this.$message.warning('请先上传一次调频测试方案和一次调频实验报告')
        return false
      } else {
        return true
      }
    },

    handleExceed() {},

    onChange1(file, fileList1) {
      // 上传单个文件且能覆盖之前的文件
      if (fileList1.length > 0) {
        this.fileList1 = [fileList1[fileList1.length - 1]] // 获取最后一次选择的文件
      }

      // 获取当前时间
      let dateTime = new Date()
      this.detailForm.regulationTime1 = this.$dayjs(dateTime).format('YYYY-MM-DD HH:mm:ss')

      // 上传文件类型的限制
      let testFile = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase()
      const extension = testFile === 'xlsx' || testFile === 'xls' || testFile === 'pdf'
      if (!extension) {
        this.$message.error('文件上传只能是 pdf/Excel(xlxs、xls) 格式!')
        return false
      } else {
        this.detailForm.regulationReportForm = file.name
        return true
      }
    },

    onChange2(file, fileList2) {
      // 上传单个文件且能覆盖之前的文件
      if (fileList2.length > 0) {
        this.fileList2 = [fileList2[fileList2.length - 1]] // 获取最后一次选择的文件
      }

      // 获取当前时间
      let dateTime = new Date()
      this.detailForm.regulationTime2 = this.$dayjs(dateTime).format('YYYY-MM-DD HH:mm:ss')

      // 判断文件类型
      let testFile = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase()
      const extension = testFile === 'xlsx' || testFile === 'xls' || testFile === 'pdf'
      if (!extension) {
        this.$message.error('文件上传只能是 pdf/Excel(xlxs、xls) 格式!')
        return false
      } else {
        this.detailForm.regulationTestPlan = file.name
        // 上传一次调频测试方案和一次调频实验报告,才能上传一次调频定值单
        if (!this.detailForm.regulationTestPlan || !this.detailForm.regulationTestReport) {
          this.flagUpload = true
        } else {
          this.flagUpload = false
        }
        return true
      }
    },

    onChange3(file, fileList3) {
      // 上传单个文件且能覆盖之前的文件
      if (fileList3.length > 0) {
        this.fileList3 = [fileList3[fileList3.length - 1]] // 获取最后一次选择的文件
      }

      // 获取当前时间
      let dateTime = new Date()
      this.detailForm.regulationTime3 = this.$dayjs(dateTime).format('YYYY-MM-DD HH:mm:ss')

      let testFile = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase()
      const extension = testFile === 'xlsx' || testFile === 'xls' || testFile === 'pdf'
      if (!extension) {
        this.$message.error('文件上传只能是 pdf/Excel(xlxs、xls) 格式!')
        return false
      } else {
        this.detailForm.regulationTestReport = file.name
        // 上传一次调频测试方案和一次调频实验报告,才能上传一次调频定值单
        if (!this.detailForm.regulationTestReport || !this.detailForm.regulationTestPlan) {
          this.flagUpload = true
        } else {
          this.flagUpload = false
        }
        return true
      }
    },

    // onChange4(file, fileList4) {
    //   // 上传单个文件且能覆盖之前的文件
    //   if (fileList4.length > 0) {
    //     this.fileList4 = [fileList4[fileList4.length - 1]]; // 获取最后一次选择的文件
    //   }

    //   // 获取当前时间
    //   let dateTime = new Date();
    //   this.detailForm.regulationTime4 = this.$dayjs(dateTime).format(
    //     "YYYY-MM-DD HH:mm:ss"
    //   );
    //   let testFile = file.name
    //     .substring(file.name.lastIndexOf(".") + 1)
    //     .toLowerCase();
    //   const extension =
    //     testFile === "xlsx" || testFile === "xls" || testFile === "pdf";
    //   if (!extension) {
    //     this.$message.error("文件上传只能是 pdf/Excel(xlxs、xls) 格式!");
    //     return false;
    //   } else {
    //     this.detailForm.responsePlan = file.name;
    //     return true;
    //   }
    // },

    // onChange5(file, fileList5) {
    //   // 上传单个文件且能覆盖之前的文件
    //   if (fileList5.length > 0) {
    //     this.fileList5 = [fileList5[fileList5.length - 1]]; // 获取最后一次选择的文件
    //   }

    //   // 获取当前时间
    //   let dateTime = new Date();
    //   this.detailForm.regulationTime5 = this.$dayjs(dateTime).format(
    //     "YYYY-MM-DD HH:mm:ss"
    //   );
    //   let testFile = file.name
    //     .substring(file.name.lastIndexOf(".") + 1)
    //     .toLowerCase();
    //   const extension =
    //     testFile === "xlsx" || testFile === "xls" || testFile === "pdf";
    //   if (!extension) {
    //     this.$message.error("文件上传只能是 pdf/Excel(xlxs、xls) 格式!");
    //     return false;
    //   } else {
    //     this.detailForm.responseReport = file.name;
    //     return true;
    //   }
    // },

    // 单机容量
    singleMachineCapChange(val) {
      if (val === undefined || this.detailForm.machineNum === undefined) {
        this.detailForm.unitsTotalCap = 0
      } else {
        this.detailForm.unitsTotalCap = val * parseInt(this.detailForm.machineNum)
      }
    },

    //台数
    stationCapChange(val) {
      if (val === undefined || this.detailForm.singleMachineCap === undefined) {
        this.detailForm.unitsTotalCap = 0
      } else {
        this.detailForm.unitsTotalCap = this.detailForm.singleMachineCap * val
      }
    },

    // 文件上传前
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'xlsx'
      if (!isJPG) {
        this.$message.error('文件上传只能是 Pdf/Excel(xlxs、xls) 格式!')
        return false
      } else {
        return true
      }
    },

    uploadHttpReq(file) {},

    //子组件校验，传递到父组件
    validateForm() {
      let flag = null
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          flag = true
        } else {
          flag = false
        }
      })
      return flag
    },

    // 电厂名称下拉框的数据
    async queryStationList() {
      let params = {
        regulationOrgId: this.detailForm.regulationOrgId,
        stationType: this.detailForm.stationType,
        ownersId: this.detailForm.companyId,
      }
      await queryStationList(params).then((res) => {
        if (res.code === '000000') {
          this.stationLists = res.data
        }
      })
    },

    // 控股单位下拉框的数据
    queryCompanyList(val) {
      queryCompanyList().then((res) => {
        if (res.code === '000000') {
          this.companyLists = res.data
          if (val) {
            this.companyLists.forEach((item) => {
              if (item.unitName == this.$refs.company.labels) this.$refs.company.labels = item.unitId
            })
          }
        }
      })
    },
    // 获取试验单位字典
    async getQueryTestingUnitList() {
      const res = await queryTestingUnitList()
      this.queryUnitList = res.data
    },
    // 获取场站名称字典
    async getQueryStationList(val) {
      let params = {
        regulationOrgId: this.detailForm.regulationOrgId,
        stationType: this.detailForm.stationType,
        ownersId: this.detailForm.companyId,
      }
      const res = await queryStationList(params)
      this.stationLists = res.data
      if (val) {
        this.stationLists.forEach((item) => {
          if (item.stationName == this.$refs.station.labels) this.$refs.station.labels = item.stationId
        })
      }
    },

    stationNameChange() {
      this.$refs.station.labels = ''
      this.queryStationList()
    },

    stationNameChange1(e) {
      this.detailForm.companyId = e
      this.$refs.station.labels = ''
      this.queryStationList()
    },
  },
}
